load motor.mat;
XtestNorm = motorNormalize(Xtest);
XtrainNorm = motorNormalize(Xtrain);


maxPower = 19;
RMSEtrain = zeros(maxPower,1);
RMSEtest = zeros(maxPower,1);

YPredictionTrain = zeros(size(XtrainNorm),2);
YPredictionTrain(:,1) = XtrainNorm;

YPredictionTest = zeros(size(XtestNorm),2);
YPredictionTest(:,1) = XtestNorm;

numDataPoints = size(XtrainNorm,1);
for powerIndex=1:maxPower
    designMatrix = ones(numDataPoints,powerIndex+1);
    for rowDesign=1:numDataPoints
        for colDesign=2:powerIndex+1
            designMatrix(rowDesign,colDesign)=XtrainNorm(rowDesign,1).^(colDesign-1);
        end
    end
    w = (designMatrix' * designMatrix)\designMatrix' * Ytrain;
    
    for i=1:size(Ytrain,1)
        phi = ones(powerIndex+1,1);
        for phiIndex=1:powerIndex+1
            phi(phiIndex,1) = XtrainNorm(i,1)^(phiIndex-1);
        end
        RMSEtrain(powerIndex,1) = RMSEtrain(powerIndex,1) + (Ytrain(i,1) - w'*phi).^2;
        YPredictionTrain(i,2) = w'*phi;
    end
    RMSEtrain(powerIndex,1) = sqrt(RMSEtrain(powerIndex,1)/size(Ytrain,1));
  
    figure(233)
    if powerIndex == 19
       plot(YPredictionTrain(:,1), YPredictionTrain(:,2)) 
    end
    
    
    for i=1:size(Ytest,1)
        phi = ones(powerIndex+1,1);
        for phiIndex=2:powerIndex+1
            phi(phiIndex,1) = XtestNorm(i,1)^(phiIndex-1);
        end
        RMSEtest(powerIndex,1) = RMSEtest(powerIndex,1) + (Ytest(i,1) - w'*phi).^2;
        YPredictionTest(i,2) = w'*phi;
    end
    RMSEtest(powerIndex,1) = sqrt(RMSEtest(powerIndex,1)/size(Ytest,1));
    RMSEtrain(powerIndex,1) = sqrt(RMSEtrain(powerIndex,1)/size(Ytrain,1));
    
    figure(234)
    if powerIndex == 19
        subplot(1, 2, 1)
       plot(YPredictionTest(:,1), YPredictionTest(:,2)) 
    end    
    if powerIndex == 12
        subplot(1, 2, 2)
       plot(YPredictionTest(:,1), YPredictionTest(:,2)) 
    end
    
end
figure(231)
subplot(1,2,1)
plot(RMSEtrain)
subplot(1,2,2)
plot(RMSEtest)


